package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.R;
import com.itheima.dto.AssignDto;
import com.itheima.dto.BusinessQueryDto;
import com.itheima.mapper.SysDeptMapper;
import com.itheima.mapper.SysDictDataMapper;
import com.itheima.mapper.TbAssignRecordMapper;
import com.itheima.mapper.TbBusinessMapper;
import com.itheima.pojo.SysDept;
import com.itheima.pojo.SysDictData;
import com.itheima.pojo.TbAssignRecord;
import com.itheima.pojo.TbBusiness;
import com.itheima.service.TbBusinessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/*
 *@Parm
 *@ClassName TbBusinessServiceImpl
 *@Date 2022/1/17 17:22
 */
@Service
public class TbBusinessServiceImpl extends ServiceImpl<TbBusinessMapper, TbBusiness> implements TbBusinessService {
   @Autowired
   private TbBusinessMapper businessMapper;

    @Override
    public R add(TbBusiness tbBusiness) {
        tbBusiness.setCreateTime(LocalDateTime.now());
        businessMapper.insert(tbBusiness);

        return R.success("添加成功");
    }

    @Override
    public R selectPage(BusinessQueryDto businessQueryDto) {
        IPage<TbBusiness> p = new Page<>(businessQueryDto.getPage(),businessQueryDto.getPageSize());
        LambdaQueryWrapper<TbBusiness> wrapper = new LambdaQueryWrapper<>();
        if (businessQueryDto.getId() != null){
            wrapper.eq(TbBusiness::getId,businessQueryDto.getId());
        }
        if (businessQueryDto.getName() != null){
            wrapper.like(TbBusiness::getName,businessQueryDto.getName());
        }
        if (businessQueryDto.getPhone() != null){
            wrapper.eq(TbBusiness::getPhone,businessQueryDto.getPhone());
        }
        if (businessQueryDto.getStatus() != null){
            wrapper.eq(TbBusiness::getStatus,businessQueryDto.getStatus());
        }
        if (businessQueryDto.getStartTime() != null ||businessQueryDto.getEndTime() != null ){
            wrapper.ge(TbBusiness::getCreateTime,businessQueryDto.getStartTime())
                    .le(TbBusiness::getCreateTime,businessQueryDto.getEndTime());
        }
        if (businessQueryDto.getCreateBy() != null){
            wrapper.eq(TbBusiness::getCreateBy,businessQueryDto.getCreateBy());
        }
        IPage<TbBusiness> IPage = businessMapper.selectPage(p, wrapper);

        return R.success(IPage);
    }

    @Autowired
    private SysDeptMapper sysDeptMapper;

    //打开分配商机，部门的显示
    @Override
    public R assign() {
        LambdaQueryWrapper<SysDept> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysDept::getParentId, 0);
        List<SysDept> sysDepts = sysDeptMapper.selectList(wrapper);
        //传智教育
        AssignDto assignDto = new AssignDto();
        for (SysDept sysDept : sysDepts) {
            assignDto.setId(sysDept.getDeptId());
            assignDto.setLabel(sysDept.getDeptName());
            LambdaQueryWrapper<SysDept> wrapper3 = new LambdaQueryWrapper<>();
            wrapper3.eq(SysDept::getParentId, sysDept.getDeptId());
            List<SysDept> sysDepts1 = sysDeptMapper.selectList(wrapper3);
            ArrayList<AssignDto> assignDtos = new ArrayList<>();
            for (SysDept sysDept1 : sysDepts1) {
                AssignDto assignDto1 = new AssignDto();
                assignDto1.setId(sysDept1.getDeptId());
                assignDto1.setLabel(sysDept1.getDeptName());
                assignDtos.add(assignDto1);
                LambdaQueryWrapper<SysDept> wrapper1 = new LambdaQueryWrapper<>();
                wrapper1.eq(SysDept::getParentId, sysDept1.getDeptId());
                List<SysDept> sysDepts2 = sysDeptMapper.selectList(wrapper1);
                ArrayList<AssignDto> assignDtos1 = new ArrayList<>();
                for (SysDept sysDept2 : sysDepts2) {
                    AssignDto assignDto2 = new AssignDto();
                    assignDto2.setId(sysDept2.getDeptId());
                    assignDto2.setLabel(sysDept2.getDeptName());
                    assignDtos1.add(assignDto2);
                    LambdaQueryWrapper<SysDept> wrapper2 = new LambdaQueryWrapper<>();
                    wrapper2.eq(SysDept::getParentId, sysDept2.getDeptId());
                    List<SysDept> sysDepts3 = sysDeptMapper.selectList(wrapper2);
                    ArrayList<AssignDto> assignDtos2 = new ArrayList<>();
                    for (SysDept sysDept3 : sysDepts3) {
                        AssignDto assignDto3 = new AssignDto();
                        assignDto3.setId(sysDept3.getDeptId());
                        assignDto3.setLabel(sysDept3.getDeptName());
                        assignDtos2.add(assignDto3);
                    }
                    assignDto2.setChildren(assignDtos2);
                }
                assignDto1.setChildren(assignDtos1);
            }
            assignDto.setChildren(assignDtos);
        }

        return R.success(assignDto);
    }

    @Autowired
    private SysDictDataMapper sysDictDataMapper;

    @Override
    public R subject() {
        LambdaQueryWrapper<SysDictData> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysDictData::getDictType,"course_subject");
        List<SysDictData> sysDictData = sysDictDataMapper.selectList(wrapper);
        return R.success(sysDictData);
    }

    @Autowired
    private TbAssignRecordMapper assignRecordMapper;
    @Override
    public R selectById(Long id) {
        LambdaQueryWrapper<TbAssignRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TbAssignRecord::getDeptId,id);
        List<TbAssignRecord> tbAssignRecords = assignRecordMapper.selectList(wrapper);

        return R.success(tbAssignRecords);
    }

    @Override
    public R modify(TbBusiness business) {
        businessMapper.updateById(business);
        return R.success("修改成功");
    }


}
